package com.cjy.hd13;

import com.google.common.collect.Lists;
import com.google.gson.Gson;

import java.util.ArrayList;

public class Student {
    private String name;
    private Integer age;
    private String sex;

    public Student(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}';
    }

    public static void main(String[] args) {
        //将Java对象转换成json字符串
        Student student = new Student("蔡佳宇", 21, "男");
        Student student1 = new Student("xxx", 21, "女");

        Gson gson = new Gson();
        String s = gson.toJson(student);
        System.out.println(s);

        ArrayList<Student> list = Lists.newArrayList(student,student1);
        System.out.println(gson.toJson(list));
        //将json字符串转换成Java对象 好处：可以快速搜索到想要的字段信息。
        //String ss = "{ }"; //字符串的内容是json的格式
        //Gson gson1 = new Gson();
        // 在这行代码加断点，单步运行，看Student变量的属性是否有值且是否与json对应的字段值一样； 调用gson对象的fromJson将json字符串转成Java对象
        //Student student2 = gson1.fromJson(ss,Student.class);
        System.out.println();
    }
}
